//
//  dcmtestviewViewController.m
//  dcmtestview
//
//  Created by wl on 9/1/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "dcmtestviewViewController.h"
#import "dcmimage.h"
#import "diregist.h" 

@implementation dcmtestviewViewController




// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	appDirectoryPrefix = [[NSString alloc] initWithString:@"~/"];
	documentsDirectoryPrefix = [[NSString alloc] initWithString:@"#/"];
	
	NSString *dcmfilePath = [self convertPath:@"~/brain_001.dcm"];
	if (![[NSFileManager defaultManager] fileExistsAtPath:dcmfilePath]) {
		return;
	}
	
	DicomImage *image = new DicomImage([dcmfilePath UTF8String]);
	if (image != NULL)
	{
		if (image->getStatus() == EIS_Normal)
		{
			int imageWidth = image->getWidth();
			int imageHeight = image->getHeight();
			cerr << "Frame count: " << image->getFrameCount() << endl;
			cerr << "Width: " << imageWidth << endl;
			cerr << "Height: " << imageHeight << endl;
			cerr << "Depth: " << image->getDepth() << endl;
			Uint8 *pixelData = (Uint8 *)(image->getOutputData(8 /* bits per sample */));
			if (pixelData != NULL)
			{
				/* do something useful with the pixel data */
				for (int i = 0; i < imageHeight ; ++i) {
					for (int j = 0; j < imageWidth; ++j) {
						//char c = *(pixelData + i * imageWidth + j);
						cerr << *(pixelData + i * imageWidth + j) << " ";
					}
					cerr << endl;
				}
			}
		} else
			cerr << "Error: cannot load DICOM image (" << DicomImage::getString(image->getStatus()) << ")" << endl;
	}
	delete image;
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (NSString *)softwareDirectory:(NSSearchPathDirectory)searchDirectory 
{
	NSString *queryDirectory = NSHomeDirectory();
	if (NSDocumentDirectory == searchDirectory)
		queryDirectory = [queryDirectory stringByAppendingPathComponent:@"Documents"];
	else if (NSApplicationDirectory == searchDirectory)
		queryDirectory = [queryDirectory stringByAppendingPathComponent:@"dcmtestview.app"];
	return queryDirectory;
}

- (NSString *)convertPath:(NSString *)pathToBeConverted
{
	NSArray *pathToBeConvertedComponents = [pathToBeConverted pathComponents];
	NSString *convertedPath = [[NSString alloc] init];
	if ([pathToBeConverted hasPrefix:appDirectoryPrefix])
		convertedPath = [[NSString alloc] initWithString:[self softwareDirectory:NSApplicationDirectory]];
	else if ([pathToBeConverted hasPrefix:documentsDirectoryPrefix])
		convertedPath = [[NSString alloc] initWithString:[self softwareDirectory:NSDocumentDirectory]];
	
	for (int i = 1; i < [pathToBeConvertedComponents count]; ++i) 
	{
		convertedPath = [convertedPath stringByAppendingPathComponent:[pathToBeConvertedComponents objectAtIndex:i]];
	}
	return convertedPath;
}

@end
